package com.example.myzhugezhaofang.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.mvplibrary.ui.activity.BaseActivity;
import com.example.myzhugezhaofang.ApiService;
import com.example.myzhugezhaofang.R;
import com.example.myzhugezhaofang.adapter.newhouse.HortAdapter;
import com.example.myzhugezhaofang.adapter.newhouse.HouseListAdapter;
import com.example.myzhugezhaofang.adapter.newhouse.SellAdapter;
import com.example.myzhugezhaofang.adapter.newhouse.TryTwoAdapter;
import com.example.myzhugezhaofang.bean.newhouse.CuTenTryTwoBean;
import com.example.myzhugezhaofang.bean.newhouse.HortBean;
import com.example.myzhugezhaofang.bean.newhouse.HouseListBean;
import com.example.myzhugezhaofang.bean.newhouse.SellTermBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.ResourceSubscriber;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class NewHouseActivity extends BaseActivity {
    @BindView(R.id.btn_back_newhouse)
    Button btn_back;
    @BindView(R.id.btn_search_newhouse)
    Button btn_search;
    @BindView(R.id.btn_message_newhouse)
    Button btn_message;
    @BindView(R.id.tv_map_newhouse)
    TextView tv_map;
    @BindView(R.id.rv_house_newhouse)
    RecyclerView rv_house;
    @BindView(R.id.rv_building_hot_list_newhouse)
    RecyclerView rv_hot_list;
    @BindView(R.id.rv_buildings_newhouse)
    RecyclerView rv_buildings;
    @BindView(R.id.rv_sellterm_newhouse)
    RecyclerView rv_sellterm;
    @BindView(R.id.iv_1_newhouse)
    ImageView iv_newhouse;
//    @BindView(R.id.smart_newhouse)
//    SmartRefreshLayout smart;


    private List<HortBean.DataBean> hortList;
    private List<SellTermBean.DataBean> sellList;
    private List<CuTenTryTwoBean.DataBean> tryTwoList;
    private List<HouseListBean.DataBean.DataBeans> houseList;
    private HortAdapter hortAdapter;
    private TryTwoAdapter tryTwoAdapter;
    private SellAdapter sellAdapter;
    private HouseListAdapter houseListAdapter;
    private String TAG = "朱勇闯NewHouse";
    private int page = 1;

    @Override
    protected void initData() {
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NewHouseActivity.this, MapActivity.class);
                startActivity(intent);
            }
        });
        Glide.with(this).load("http://img.zgsta.zhuge.com/xfcrm/2021-04-15/2b55b467b49ed046c42e0932636ae97a.jpeg").into(iv_newhouse);
//全部楼盘
        initHort();
//最新取证
        initSellTerm();
//最新取证
        initTryTwo();
//houseList
        initHouseList(page);
    }

    private void initHort() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("city", "bj");
        map.put("platformType", "4");
        map.put("on_off", "1");
        new Retrofit.Builder()
                .baseUrl(ApiService.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(ApiService.class)
                .getsHortcuTenTry(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new ResourceSubscriber<HortBean>() {
                    @Override
                    public void onNext(HortBean listBean) {
                        hortList = listBean.getData();
                        LinearLayoutManager manager = new LinearLayoutManager(NewHouseActivity.this);
                        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                        rv_buildings.setLayoutManager(manager);
                        hortAdapter = new HortAdapter(hortList, NewHouseActivity.this, R.layout.item_hort);
                        rv_buildings.setBackground(null);
                        rv_buildings.setAdapter(hortAdapter);
//                        rv_buildings.addItemDecoration(new DividerItemDecoration(NewHouseActivity.this, DividerItemDecoration.VERTICAL));
                        hortAdapter.setListener(new HortAdapter.onItemListener() {
                            @Override
                            public void onClick(int n) {
                                String url = hortList.get(n).getUrl();
                                Intent intent = new Intent(NewHouseActivity.this, WebActivity.class);
                                intent.putExtra("link", url);
                                startActivity(intent);
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable t) {
                        Log.e(TAG, "onError: " + t.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initSellTerm() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("city", "bj");
        map.put("platformType", "4");
        map.put("type", "2");
        map.put("platform_type", "4");
        new Retrofit.Builder()
                .baseUrl(ApiService.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(ApiService.class)
                .getSellTerm(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new ResourceSubscriber<SellTermBean>() {
                    @Override
                    public void onNext(SellTermBean listBean) {
                        sellList = listBean.getData();
                        LinearLayoutManager manager = new LinearLayoutManager(NewHouseActivity.this);
                        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                        rv_sellterm.setLayoutManager(manager);
                        sellAdapter = new SellAdapter(sellList, NewHouseActivity.this, R.layout.item_sellterm);
                        rv_sellterm.setAdapter(sellAdapter);
//                        rv_buildings.addItemDecoration(new DividerItemDecoration(NewHouseActivity.this, DividerItemDecoration.VERTICAL));
                        sellAdapter.setListener(new SellAdapter.onItemListener() {
                            @Override
                            public void onClick(int n) {
                                String url = sellList.get(n).getUrl();
                                Intent intent = new Intent(NewHouseActivity.this, WebActivity.class);
                                intent.putExtra("link", url);
                                startActivity(intent);
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable t) {
                        Log.e(TAG, "onError: " + t.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initTryTwo() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("city", "bj");
        map.put("platformType", "4");
        new Retrofit.Builder()
                .baseUrl(ApiService.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(ApiService.class)
                .getShortCuTenTryTwo(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new ResourceSubscriber<CuTenTryTwoBean>() {
                    @Override
                    public void onNext(CuTenTryTwoBean listBean) {
                        tryTwoList = listBean.getData();
                        LinearLayoutManager manager = new GridLayoutManager(NewHouseActivity.this, 2);
                        rv_hot_list.setLayoutManager(manager);
                        tryTwoAdapter = new TryTwoAdapter(tryTwoList, NewHouseActivity.this, R.layout.item_trytwo);
                        rv_hot_list.setAdapter(tryTwoAdapter);
//                        rv_buildings.addItemDecoration(new DividerItemDecoration(NewHouseActivity.this, DividerItemDecoration.VERTICAL));
                        tryTwoAdapter.setListener(new TryTwoAdapter.onItemListener() {
                            @Override
                            public void onClick(int n) {
                                String url = tryTwoList.get(n).getUrl();
                                Intent intent = new Intent(NewHouseActivity.this, WebActivity.class);
                                intent.putExtra("link", url);
                                startActivity(intent);
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable t) {
                        Log.e(TAG, "onError: " + t.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initHouseList(int page) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("city", "bj");
        map.put("page", page);
        map.put("limit", "10");
        map.put("operaVersion", "5.1.1");
        map.put("token", "yQgzcPDPv7uFGMFHdugwIKLSFjE95SmFTBR_GN55bVhyUjxcihKWmb9z-ASbrO52oFiwaOHgdE3dgrYkyJpiGQ%3D%3D");
        map.put("platformType", "4");
        map.put("platform_type", "4");
        new Retrofit.Builder()
                .baseUrl(ApiService.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(ApiService.class)
                .getHouseList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new ResourceSubscriber<HouseListBean>() {
                    @Override
                    public void onNext(HouseListBean listBean) {
                        ArrayList<HouseListBean.DataBean.DataBeans> list = (ArrayList<HouseListBean.DataBean.DataBeans>) listBean.getData().getData();
                        houseList = new ArrayList<>();
                        houseList.addAll(list);
                        Log.e(TAG, "onNext: " + houseList.size());
                        LinearLayoutManager manager = new LinearLayoutManager(NewHouseActivity.this);
                        rv_house.setLayoutManager(manager);
                        houseListAdapter = new HouseListAdapter(houseList, NewHouseActivity.this, R.layout.item_newhouse_houselist);
                        rv_house.setAdapter(houseListAdapter);
                        rv_house.addItemDecoration(new DividerItemDecoration(NewHouseActivity.this, DividerItemDecoration.VERTICAL));
                        houseListAdapter.setListener(new HouseListAdapter.onItemListener() {
                            @Override
                            public void onClick(int n) {
                                Toast.makeText(NewHouseActivity.this, "后面内容尚未更新，请亲们稍等待", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable t) {
                        Log.e(TAG, "onError: " + t.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

   /* private void initSmart() {
        smart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                initHouseList(page);
                houseListAdapter.notifyDataSetChanged();
                initFinish();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                houseList.clear();
                initHouseList(page);
                houseListAdapter.notifyDataSetChanged();
                initFinish();
            }
        });
    }

    private void initFinish() {
        smart.finishRefresh();
        smart.finishLoadMore();
    }*/

    @Override
    protected int createLayoutId() {
        return R.layout.activity_new_house;
    }
}